package com.service.system.impl;

import com.dao.system.ModuleDao;
import com.dao.system.RoleDao;
import com.dao.system.UserDao;
import com.domain.system.Module;
import com.domain.system.User;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.service.system.UserService;
import com.utils.MD5Util;
import com.utils.MapperUtils;
import org.apache.ibatis.session.SqlSession;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

public class UserServiceImpl implements UserService {
    /**
     * 查询全部
     * @return
     */
    @Override
    public List<User> findAll() {
        List<User> list = null;
        try {
            SqlSession session= MapperUtils.init();
            UserDao mapper = session.getMapper(UserDao.class);
            list = mapper.findAll();
            MapperUtils.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     * 分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageInfo findAll(int page, int size) {
        PageInfo pageInfo = null;
        try {
            SqlSession session = MapperUtils.init();
            UserDao mapper = session.getMapper(UserDao.class);
            Page pageHelper = PageHelper.startPage(page,size);
            mapper.findAll();
            pageInfo = new PageInfo(pageHelper);
            //释放资源
            MapperUtils.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return pageInfo;
    }

    /**
     * 根据id查找
     * @param id
     * @return
     */
    @Override
    public User findById(String id) {
        User user = null;
        try {
            SqlSession session = MapperUtils.init();
            UserDao mapper = session.getMapper(UserDao.class);
            user = mapper.findById(id);
            MapperUtils.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return user;
    }

    /**
     * 保存
     * @param user
     */
    @Override
    public void save(User user) {
        try {
            SqlSession session = MapperUtils.init();
            UserDao mapper = session.getMapper(UserDao.class);
            String id = UUID.randomUUID().toString();
            String pass = MD5Util.md5(user.getPassword());
            user.setPassword(pass);
            user.setId(id);
            mapper.save(user);
            MapperUtils.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 删除
     * @param user
     */
    @Override
    public void delete(User user) {
        try {
            SqlSession session = MapperUtils.init();
            UserDao mapper = session.getMapper(UserDao.class);
            mapper.delete(user);
            MapperUtils.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 修改
     * @param user
     */
    @Override
    public void update(User user) {
        try {
            SqlSession session = MapperUtils.init();
            UserDao mapper = session.getMapper(UserDao.class);
            mapper.update(user);
            MapperUtils.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void updateRole(String userId, String[] roleIds) {
        try {
            SqlSession session = MapperUtils.init();
            UserDao mapper = session.getMapper(UserDao.class);
            //将当前角色与用户有的关系删除
            mapper.deleteUserByRole(userId);

            //因为可能存在有多个关联,所以循环取出
            for (String roleId : roleIds) {
                //建立新的关联
                mapper.saveUserRole(userId,roleId);
            }
            MapperUtils.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public User findByLogin(String email, String password) {
        User user = null;
        try {
            SqlSession session = MapperUtils.init();
            UserDao mapper = session.getMapper(UserDao.class);
            //给密码加密
            password = MD5Util.md5(password);
            user = mapper.findByLogin(email,password);
            MapperUtils.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return user;
    }

    @Override
    public List<Module> findModuleByUserId(String id) {
        List<Module> modules = null;
        try {
            SqlSession session = MapperUtils.init();
            ModuleDao mapper = session.getMapper(ModuleDao.class);

            modules = mapper.findModuleByUserId(id);
            MapperUtils.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return modules;
    }
}
